package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class UpdateDemo {
    public static void main(String[] args) throws Exception {
        System.out.println("修改用户信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入新的密码");
        String password = scanner.nextLine();
        System.out.println("请输入新的昵称");
        String nickname = scanner.nextLine();

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        Statement statement = connection.createStatement();
        String sql = "UPDATE user " +
                "SET password='"+password+"',nickname='"+nickname+"' " +
                "WHERE username='"+username+"'";
        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "修改成功！" : "修改失败！");
        connection.close();
    }
}
